<link rel="stylesheet" href="css/spa_guestList/spa_guestList.css"/>
<link rel="stylesheet" href="assets/css/ui.jqgrid.css"/>
<title data-i18n="前台接待">前台接待</title>
<div class="row">
	<div id="pageBox"></div>   
	<div class="no-padding"> 
		   <div class="well well-sm" style="margin-bottom:0px;"> 
			    <div class="clearfix" style="margin-left: 35px;">   
				     <div class="pull-left frontspa-search"> 
				      	  <input autofocus="autofocus" type="text" id="inputValue" name="th" autocomplete="off" data-i18n-placeholder="手牌" class="query-item enter_query" style="height:30px;" placeholder="手牌" /> 
					      <div class="btn-group"> 
					       	<button id="guestListbtnSearch" class="btn btn-sm btn-info" data-i18n="查找" style="margin-top:-4px;margin-left:20px;"><i class="ace-icon fa fa-search"></i>查找</button> 
					      </div>
					      <div class="btn-group"> 
					       	<button id="btnRefresh" class="btn btn-sm btn-info" data-i18n="刷新" style="margin-top:-4px;margin-left:2px;"><i class="ace-icon fa fa-refresh"></i>刷新</button> 
					      </div>  
				     </div> 
				     <div class="pull-right">
				    		<label class="control-label"> 
								 <input name="status_radio" value="btnJdh" type="radio" class="ace" checked="checked"> 
								 <span class="lbl" data-i18n="手牌列表">手牌列表</span> 
							 </label>
							 <label class="control-label"> 
								 <input name="status_radio" value="btnJdhDetail" type="radio" class="ace"> 
								 <span class="lbl" data-i18n="销售明细">销售明细</span> 
							 </label>
				     		 
					      <div class="btn-group"> 
					       	<button id="btnBack" class="btn btn-sm btn-default" data-i18n="返回"> <i class="ace-icon fa fa-undo"></i> 返回</button> 
					      </div> 
				     </div> 
			    </div> 
		   </div> 
	</div>
	<div class="accordion-style1 panel-group guestListContent"> 
		
	</div> 
</div>
<div class="toolBar">
    <span id="" class="label a4169E1 arrowed-right" style="color:#FFFFFF !important;background-color:#4169E1 !important;">统计</span><!-- height: !important;undefined -->
</div>

<script type="text/javascript"> 
    //加载资源文件
    var scripts =   ['js/plugin/jsonsql.js',
                     'js/spa_guestList/jquery.guestList.block.js',
                     'js/spa_guestList/jquery.plugin.guest_xsxm.js',
                     'js/spa_guestList/jquery.plugin.th_search.js',
                     'assets/js/jquery.hotkeys.js',
                    ];
    //全窗口变量
	var ins_argjson={};
	ins_argjson.onSelOper=="btnJdh";
    $(".page-content-area").ace_ajax("loadScripts", scripts, function() {
		//事件绑定
		bindEvent();
		//初始化
		ins_argjson.onSelTh=sysGetQueryString("th")?sysGetQueryString("th"):"";
		var onSelJdh=sysGetQueryString("jdh")?sysGetQueryString("jdh"):""; 
		if(onSelJdh!="")
			ins_argjson.onSelJdh=onSelJdh; 
		getData({"operation":"刷新","th":ins_argjson.onSelTh,"jdh":""});     
		reSetInput("");

		$("#pageBox").draggable({addClasses: false });
		$("#pageBox").th_search({  
			top:"-8px", 
			dataBack : function(value){
				getData({"operation":"查询","th":value,"jdh":""});
			},
		})
    });

    function getData(jsonParams){
		var param={};
		var arg={"arg":"前台接待"};
		arg=$.extend({},arg,jsonParams); 
		param.arg=arg;
        $.HSAPI_CALLPROCEDURE2(param,"SPAOrderSynSearch",{},function(data){
            if(data.RETURN==1){
                if(jsonParams.operation=="刷新"){ 
                    $(".guestListContent").empty(); 
                    jdhList(data.jdh);
                    if(!ins_argjson.hasOwnProperty("onSelJdh"))
                        ins_argjson.onSelJdh=$($(".stylea")[0]).attr("data-id");
                    jdhh(data.ct_brzd_jdh,function(){
                        $($("a[data-id='"+ins_argjson.onSelJdh+"']").attr("href")).collapse('show');
                        if($($("a[data-id='"+ins_argjson.onSelJdh+"']").attr("href")).find(".selected").length==0){
                            var dom=$("a[data-id='"+ins_argjson.onSelJdh+"']").attr("href"); 
                            $(dom).find(".panel-body").children("div:first-child").addClass("selected");
                            ins_argjson.onSelTh=$(dom).find(".panel-body").children("div:first-child").attr("data-body-th");
                        }
                    });
                }
                if(jsonParams.operation=="查询"){
                    //刷新th所在jdh 
                    if(data.th!=""){
                        ins_argjson.onSelJdh=data.ct_group[0].jdh;
                        ins_argjson.onSelTh=data.th;
                        jdhList(data.ct_group);
                    }else{
                        ins_argjson.onSelTh=data.ct_brzd_jdh[0].th;  
                    }

                    //明细
                    if(ins_argjson.onSelOper=="btnJdhDetail"){
                        jdhDetails(data);
                        reSetInput("");
                        return; 
                    }
                    //手牌
                    jdhh(data.ct_brzd_jdh);
                    reSetInput("");
                }
            }else{
                if(data.hasOwnProperty("gritter")){
                    if(data.hasOwnProperty("gritterstyle"))
                        jf_successDelayed(data.gritter,2000,data.gritterstyle);
                    else
                        jf_successDelayed(data.gritter,2000); 
                }else{
                    $.HSAPI_ALERT(data.MSG)
                }
            }
        },function(data){
            jf_alert(JSON.stringify(data));
        });
	}
	
	//生成dom
	function jdhList(data){
		var map1 = data.map(x => {
			var uuid=getUUID();
			var item='<div data-jdh="'+x.jdh+'" class="panel panel-default">'+
				   		'<div class="panel-heading">'+
							'<div class="profile-activity clearfix">'+  
							   '<div>'+ 
							    '<a class="accordion-toggle collapsed stylea" data-toggle="collapse" data-parent="#accordion" data-id="'+x.jdh+'" href="#'+uuid+'" aria-expanded="false">'+
							    	'<i class="bigger-110 ace-icon fa fa-angle-down" data-icon-hide="ace-icon fa fa-angle-down" data-icon-show="ace-icon fa fa-angle-right"></i>'+
							    	'<span class="collapsedTitle">接待号:</span><span class="collapsedText">'+x.jdh+'</span>&nbsp'+
							    	'<span class="collapsedTitle">消费金额:</span><span class="collapsedText">'+x.xfje+'</span>&nbsp'+
							    	'<span class="collapsedTitle">数量:</span><span class="collapsedText">'+x.quantity+'</span>&nbsp'+
						    	'</a>'+ 
							   '</div>'+ 
							   '<div class="tools action-buttons">'+   
								    '<a id="'+getUUID()+'" data-edit-id="'+uuid+'" data-jdh="'+x.jdh+'" href="javaScript:void(0);" class="blue">'+
								    	'<i class="ace-icon fa fa-pencil bigger-170"></i>'+ 
								    '</a>'+  
								    '<a style="margin-left:40px;" id="'+getUUID()+'" data-ledger-id="'+uuid+'" data-jdh="'+x.jdh+'" href="javaScript:void(0);" class="blue">'+
								    	'<i class="ace-icon fa fa-jpy bigger-170"></i>'+ 
								    '</a>'+
							   '</div>'+ 
							  '</div>'+
				    	'</div>'+ 
				    	'<div class="panel-collapse collapse clearfix" id="'+uuid+'" aria-expanded="false">'+     
						     '<div data-id="'+uuid+'" class="panel-body" style="padding: 0px;">'+'</div>'+    
				    	'</div>'+ 
				   '</div>';
				   if($("div[data-jdh='"+x.jdh+"']").length==1){   
					   var brother=$("div[data-jdh='"+x.jdh+"']").prev();   
					   $("div[data-jdh='"+x.jdh+"']").remove();
					   if(brother.length==1){
						   $(item).insertAfter(brother);
					   } else
						   $(".guestListContent").prepend(item);
					   $("#"+uuid).collapse('show');
				   }else
					   $(".guestListContent").append(item);  
				   
				   $("a[href='#"+uuid+"']").bind("click",function(){   
					   ins_argjson.onSelJdh=$(this).attr("data-id");
					   if($(this).hasClass("collapsed")){
						   getData({"operation":"查询","th":"","jdh":ins_argjson.onSelJdh});  
						   return;
					   }
					   $("div[data-id='"+uuid+"']").empty();  
				   }); 
				   
				   $("a[data-edit-id='"+uuid+"']").bind("click",function(){
					    var jdh=$(this).attr("data-jdh");
					    $(this).modalFrame("destroy");
						$(this).modalFrame("init",function(modalEle){   
                            $("#"+modalEle).modal("show"); 
                            $("#"+modalEle).css("display","block");
							if(!$.isFunction($().actGroupSave)){
                                $.ajaxSetup({ async: false, cache: false });
					            $.getScript("js/spa_guestList/jquery.actGroupSave.js");
					            $.ajaxSetup({ async: true });
							}
							$("#"+modalEle).actGroupSave({
								data:x,
								dataBack : function(){
									$("#"+modalEle).modal("hide"); 
									$("#"+modalEle).empty();
									
									$("a[href='#"+uuid+"']").trigger("click"); 
								},
								returnBack : function(){ 
									$("#"+modalEle).modal("hide");
									$("#"+modalEle).empty();
								}
							})
						});
				   });
				   
				   $("a[data-ledger-id='"+uuid+"']").bind("click",function(){
					   var params="?&th="+ins_argjson.onSelTh+"&jdh="+ins_argjson.onSelJdh;
					//    replaceUrl("home#page/spa_guestList"+params); 
                    //    window.open("home#page/spa_guestAccount"+params,"_self");  
						// $.HSAPI_ALERT("跳转待做");    
						window.location.href = "#/html/cashier_mana/spa_guestAccount.html"+params 
				   });
				
		});
		
		
	}
	
	//手牌刷新
	function jdhh(data,dataBack){ 
		var dom=$("a[data-id='"+ins_argjson.onSelJdh+"']").attr("href");
		$(dom).find(".panel-body").empty();
		$("").blocks({
			data:data,
			oper:"jdh",
			dataBack:function(backData){
				$(dom).find(".panel-body").append(backData);
				$(dom).find(".panel-body").children("div:last-child").bind("click",function(){ 
					if($(this).hasClass("selected"))
						return;
					$(".selected").css("border-color",$(".selected").attr("data-default-color"));
					$(".selected").removeClass("selected");
					$(this).addClass("selected");
					$(this).css("border-color",$(this).attr("data-focus-color"));
					ins_argjson.onSelJdh=$(this).attr("data-body-jdh");
					ins_argjson.onSelTh=$(this).attr("data-body-th");
				});
				if($(dom).find(".panel-body").children("div:last-child").attr("data-body-th")==ins_argjson.onSelTh){
					$(".selected").css("border-color",$(".selected").attr("data-default-color"));
					$(".selected").removeClass("selected");
					$(dom).find(".panel-body").children("div:last-child").addClass("selected");
					$(dom).find(".panel-body").children("div:last-child").css("border-color",$(dom).find(".panel-body").children("div:last-child").attr("data-focus-color"));
				}
			},
			workComplete:function(){  
				reSetScrollTop();
				if($.isFunction(dataBack))
					dataBack();
			}
		})
	}
	
	//明细刷新
	function jdhDetails(data){ 
		var dom=$("a[data-id='"+ins_argjson.onSelJdh+"']").attr("href"); 
		var brzd_json={};
		$.each(data.ct_brzd_jdh,function(key,value){  
			brzd_json[value.th] = value
		})

		$(dom).find(".panel-body").empty();
		$(dom).find(".panel-body").guest_xsxm({ 
			xsxm_jdh_array:data.ct_brxsxm_jdh, 
			brzd_json:brzd_json,
			dblClickBack : function(data){
				
			},
			loadcomplete : function(){
				reSetScrollTop();
			}
		})
	}
	
	//页面事件绑定
	function bindEvent(){
		//视图
		$("input[name='status_radio']").bind("click",function(){
			   ins_argjson.onSelOper=$(this).attr("value");
			   getData({"operation":"查询","th":"","jdh":ins_argjson.onSelJdh}); 
	    })
		
		//查询按钮
		$("#guestListbtnSearch").click(function(){ 
			$(".panel-collapse").collapse('hide'); 
			var th=$("input[name='th']").val();
			if(th==="")
				getData({"operation":"查询","th":ins_argjson.onSelTh,"jdh":""}); 
			else
				getData({"operation":"查询","th":th,"jdh":""});
		})
		
		//返回按钮
		$("#btnBack").click(function(){
			history.go(-1);
		})
		
		//回车事件
		$("#inputValue").keydown(function(e){
			if(e.keyCode==13){
				$("#guestListbtnSearch").trigger("click");
			}
		})
		
		//刷新
		$("#btnRefresh").bind("click",function(){
			getData({"operation":"刷新","th":ins_argjson.onSelTh,"jdh":""}); 
		});
		
		//客房状态
		$("body,input[name='th']").bind('keydown', 'Alt+t', function (evt){
			$("#btnRefresh").trigger("click");
			return false;
		});
	}
	
	//交互等待
	function wait(){
		$("#pageOverlay").removeClass("hide");
		$("#loading").removeClass("hide");
	}
	
	//交互结束
	function goon(){
		$("#pageOverlay").addClass("hide");
		$("#loading").addClass("hide");
	}
	
	//清空条件
	function reSetInput(value){
		$("input[name='th']").val(value);
		$("input[name='th']").focus();
	}
	
	//滚动条位置
	function reSetScrollTop(){ 
		if(typeof(ins_argjson.onSelJdh)!="undefined"){
			$(".guestListContent").scrollTop(0); 
			var topHeithg=$("div[data-jdh='"+ins_argjson.onSelJdh+"']").offset().top;
			$(".guestListContent").scrollTop(topHeithg-200);   
		}
	}
	
	//欸一标识
	function getUUID() {
	    var d = new Date().getTime();
	    if (window.performance && typeof window.performance.now === "function") {
	        d += performance.now();
	    }
	    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
	        var r = (d + Math.random() * 16) % 16 | 0;
	        d = Math.floor(d / 16);
	        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
	    });
	    return uuid;
    }
    
</script>
